package com.lemon.smartsafety.ui.adapter.safe.safe_manager;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.CardView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.entity.safe.safe_manager.CompanySafeManageItemVo;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafety.utils.common.DateUtils;
import com.lemon.smartsafety.utils.common.StringUtils;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Cooliean on 16/3/29.
 */
public class SafeManagerItemAdapter extends LemonRcvAdapter<CompanySafeManageItemVo> {
    private RecyclerTClickListener<CompanySafeManageItemVo> mListener;

    public SafeManagerItemAdapter(@Nullable List<CompanySafeManageItemVo> data) {
        super(data);
    }

    public SafeManagerItemAdapter(List<CompanySafeManageItemVo> data, RecyclerTClickListener<CompanySafeManageItemVo> listener) {
        super(data);
        this.mListener = listener;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new AdapterItemView();
    }

    public class AdapterItemView implements AdapterItem<CompanySafeManageItemVo> {


        @Bind(R.id.tv_item_title)
        TextView tvItemTitle;
        @Bind(R.id.tv_check_cycle)
        TextView tvCheckCycle;
        @Bind(R.id.tv_last_check)
        TextView tvLastCheck;
        @Bind(R.id.tv_remark)
        TextView tvRemark;
        @Bind(R.id.tv_status_tip)
        TextView tvStatusTip;
        @Bind(R.id.tv_go_detail)
        TextView tvGoDetail;
        @Bind(R.id.main_card_view)
        CardView mainCardView;
        @Bind(R.id.iv_status_light)
        ImageView ivStatusLight;
        @Bind(R.id.imageArrow)
        ImageView imageArrow;
        private CompanySafeManageItemVo mData;
        private int mPosition;


        @Override
        public int getLayoutResId() {
            return R.layout.item_safe_manager_item;
        }

        @Override
        public void bindViews(View root) {
            ButterKnife.bind(this, root);
        }

        @Override
        public void setViews() {
            mainCardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(mData, mPosition);

                }
            });
//            tvGoDetail.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    System.out.println("detail");
//
//                }
//            });
        }

        @Override
        public void handleData(CompanySafeManageItemVo data, int position) {
            mData = data;
            mPosition = position;

            tvItemTitle.setText(StringUtils.nullValue(data.getItem_name()));
            tvCheckCycle.setText(String.format("检查周期：%d%s", data.getCheck_freq(), StringUtils.nullValue(data.getCheck_freq_unit_name())));
            tvLastCheck.setText(String.format("上次检查：%s %s", StringUtils.nullValue(data.getLast_check_user(), "--"),
                    DateUtils.getFormatDateStringWithDateString(data.getLast_check_time(), DateUtils.dateFormatYMDHM)));
            tvRemark.setText(String.format("备注：%s", StringUtils.nullValue(data.getRemark(), "--")));

            String checkResult = StringUtils.nullValue(data.getCheck_result());
            boolean isOK=false;
            if (checkResult.length() != 0 && checkResult.indexOf(",") != -1) {
                checkResult = checkResult.substring(checkResult.indexOf(",") + 1);
            }
            if(data.getCheck_result().contains("1")){
                isOK=true;
            }else  if(data.getCheck_result().contains("0")){
                isOK=false;
            }else{
                isOK=true;
            }



            StringUtils.ColorStatus status=StringUtils.ColorStatus.STATUS_COMMON;

            //检查项目检查状态  -1为超时检查，黄灯；1为按时检查，绿灯
            if(!isOK){
                ivStatusLight.setBackgroundResource(R.drawable.shape_circle_red);
                status=StringUtils.ColorStatus.STATUS_RED;
            }else {
                switch (data.getCheck_status()) {
                    case 1:
                        ivStatusLight.setBackgroundResource(R.drawable.shape_circle_yellow);
                        status=StringUtils.ColorStatus.STATUS_YELLOW;
                        break;
                    case 0:
                        ivStatusLight.setBackgroundResource(R.drawable.shape_circle_yellow);
                        status=StringUtils.ColorStatus.STATUS_YELLOW;
                        break;
                    case 2:
                        ivStatusLight.setBackgroundResource(R.drawable.shape_circle_green);
                        status=StringUtils.ColorStatus.STATUS_GREEN;
                        break;
                    default:
                        ivStatusLight.setBackgroundResource(R.drawable.shape_circle_white);
                        break;
                }
            }


            if (data.getCheck_status() == 0) {
                tvStatusTip.setText(StringUtils.getStatusString("未检查   "+checkResult, status));
                tvGoDetail.setVisibility(View.GONE);
            } else if (data.getCheck_status() == 1) {
                tvStatusTip.setText(StringUtils.getStatusString("已超时   "+checkResult, status));
                tvGoDetail.setVisibility(View.VISIBLE);
            } else if (data.getCheck_status() == 2) {
                tvStatusTip.setText(StringUtils.getStatusString("已检查   "+checkResult, status));
                tvGoDetail.setVisibility(View.VISIBLE);
            }
            tvGoDetail.setText("查看详情>>");


        }
    }
}
